var params = JSON.parse(window.localStorage.users)
console.log(params)
var id = params.id;
var name = params.name
var access_token = params.access_token
document.title = name
//get页面数据
$.ajax({
    type: "POST",
    url: api('/stock-bill/get-putaway-list'),
    data: {
        access_token:access_token,
        id:id,
    },
    dataType: "json",
    success: function (res) {
        console.log(res)
        stockList(res.data.list)
    }
});
//渲染上架页面
function stockList(data) {
    var html = ''
    $.each(data, function (index, item) {
        // console.log(item)
        html += `<li class="food-item" data-id="${item.item_id}">
            <div class="food-image">
              <img src="${item.img}" alt="">
            </div>
            <div class="food-typeFace">
              <div>${item.item_name}</div>
              <div>应补:<span class="show_num">${item.num}</span></div>
              <input class="placeholder" type="tel" placeholder ='请输入上架数量' onfocus="enterHandle(this)" onblur="comeHandle(this)">
            </div>
          </li>`
          $('.food-list').html(html)
    })
}
//监听input框状态
var input_num
function enterHandle(item) {
    input_num = item.value
    if(item.value){
        item.value= ''
        item.placeholder ='请输入上架数量'
    }
}
function comeHandle(item){
    // console.log(input_num.parseInt())
    var num = parseInt(input_num.replace(/[^0-9]/ig,""))
    if(item.value){
        item.value='当前:'+item.value+''
        item.style.border = '1px solid #f5a623'
    }else if(!num){
        item.placeholder ='请输入上架数量'
    } else {
        item.value='当前:' + num +''
    }
}
//点击post数据
function seveHandle(){
    var data = []
    var count = 0,
        length = ''
    $('input').each(function (i,item) {
        if(!item.value){
            count++
        }
        length = i+1
    })
    if(count == length){
        layer.open({
            content: '请输入铺货的数量'
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    }else {
        $('input').each(function (i,item) {
            var num
            if(!item.value){
                num = 0
            }else {
                num = item.value.split(':')[1]
            }

            var food = {}
            food.id = $(this).parents('.food-item').attr('data-id')
            food.show_num = $(this).parents('.food-typeFace').find('.show_num').text()
            food.name = $(this).parents('.food-item')[0].innerText.split('\n')[1]
            food.num = num
            console.log(food)
            data.push(food)
        })
        console.log(data)
        var jsonData = JSON.stringify(data)
        layer.open({
            content: '请确认上架产品数量是否正确'
            // '请确认上架产品数量是否正确'
            // '请核对所有产品库存' '重新核对''以核对准确'
            ,btn: ['确定提交', '再看一下']
            ,yes: function(index){
                console.log(index)
                foodPost(jsonData)
                layer.close(index);
            }
        });
    }
}
//post数据
function foodPost(data) {
    console.log(data)
    $.ajax({
        type: "POST",
        url: api('/stock-bill/create'),
        data: {
            access_token:access_token,
            id:id,
            type:'2',
            items:data,
        },
        dataType: "json",
        success: function (res) {
            // console.log(res)
            if(res.success){
                window.location.href = './stock.html?v='+ random +''
            }else {
                layer.open({
                    className: 'popuo-login',
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }
    });
}
